package com.abel.bigwater.data.kente

import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat

/**
 *
val str = "ADDRESS:001 NAME:10001062 FTOTAL:0000423361.9070M3 RTOTAL:0000000074.2690M3 FLOW:+37.1371M3/h SPEED:0.3283m/s TUB:00000% ALARM:550000000 CSQ:25 TIME:2018-07-25 16:30:05 PRESS:0.3131MPa"
 */
class KenteDatagram : KentePacket {
    companion object {
        const val head = "ADDRESS:"

        const val KEY_NAME = "NAME:"

        const val KEY_FTOTAL = "FTOTAL:"

        const val KEY_RTOTAL = "RTOTAL:"

        const val KEY_FLOW = "FLOW:"

        const val KEY_SPEED = "SPEED:"

        const val KEY_TUB = "TUB:"

        const val KEY_ALARM = "ALARM:"

        const val KEY_CSQ = "CSQ:"

        const val KEY_TIME = "TIME:"

        const val KEY_PRESS = "PRESS:"

        private val lgr = LoggerFactory.getLogger(KenteDatagram::class.java)
    }

    var addr: String? = null

    var tub: Double? = null

    var alarm: String? = null

    var csq: Long? = null

    /**
     * "ADDRESS:001 NAME:10001062 FTOTAL:0000423361.9070M3 RTOTAL:0000000074.2690M3 FLOW:+37.1371M3/h SPEED:0.3283m/s TUB:00000% ALARM:550000000 CSQ:25 TIME:2018-07-25 16:30:05 PRESS:0.3131MPa"
     */
    constructor(_msg: String) {
        msg = _msg

        var datePart: String? = null
        for (str1 in msg!!.split(" ")) {
            str1.trim().also {
                when {
                    it.startsWith(head) -> addr = it.drop(head.length)

                    it.startsWith(KEY_NAME) -> rtuId = it.drop(KEY_NAME.length).toLong()

                    it.startsWith(KEY_FTOTAL) -> forwardReading = it.drop(KEY_FTOTAL.length).dropLast(2).toDouble()

                    it.startsWith(KEY_RTOTAL) -> revertReading = it.drop(KEY_RTOTAL.length).dropLast(2).toDouble()

                    it.startsWith(KEY_FLOW) -> flow = it.drop(KEY_FLOW.length).dropLast(4).toFloat()

                    it.startsWith(KEY_SPEED) -> speed = it.drop(KEY_SPEED.length).dropLast(3).toFloat()

                    it.startsWith(KEY_TUB) -> tub = it.drop(KEY_TUB.length).dropLast(1).toDouble() / 100.0

                    it.startsWith(KEY_ALARM) -> {
                        alarm = it.drop(KEY_ALARM.length)
                        val tmp = alarm!!
                        if (tmp.length > 0) super.meterVolt = tmp[0].toShort()
                        if (tmp.length > 1) super.rtuVolt = tmp[1].toShort()
                        if (tmp.length > 2) super.commError = tmp[6].toByte()
                        if (tmp.length > 3) super.emptyError = tmp[5].toByte()
                        if (tmp.length > 4) super.magnetError = tmp[4].toByte()
                        if (tmp.length > 5) super.flowMaxError = tmp[3].toByte()
                        if (tmp.length > 6) super.flowMinError = tmp[2].toByte()
                        if (tmp.length > 7) super.pressureMaxError = tmp[1].toByte()
                        if (tmp.length > 8) super.pressureMinError = tmp[0].toByte()
                    }

                    it.startsWith(KEY_CSQ) -> super.rssi = it.drop(KEY_CSQ.length).toShort()

                    it.startsWith(KEY_TIME) -> datePart = it.drop(KEY_TIME.length)

                    it.startsWith(KEY_PRESS) -> pressure = it.drop(KEY_PRESS.length).dropLast(3).toFloat()

                    else -> {
                        // may be time
                        if (datePart != null) {
                            sampleTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("$datePart $it")
                        }
                    }
                }
            }
        }
    }
}